/**
 * Package:com.goldcitynet.engine.api.as3.service;
 * $Id$
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.engine.api.as3.service;

import com.afteryuan.engine.api.as3.dto.As3Scene;
import com.afteryuan.engine.api.as3.dto.As3Component;
import com.afteryuan.engine.model.SceneComponentRelation;
import com.afteryuan.engine.model.SceneInstance;
import com.afteryuan.engine.service.SceneComponentRelationService;
import com.afteryuan.engine.service.SceneInstanceService;
import com.afteryuan.engine.service.ComponentService;
import com.afteryuan.util.Configure;

import java.util.List;
import java.util.ArrayList;


/**
 * 用As3 RPC方式提供的场景API
 * <p/>
 * <p><a href="SceneFlexService.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author$</a>
 * @version $Revision$
 */
public class SceneFlexService extends AbstractFlexService {
    private ComponentService componentService;
    private SceneInstanceService sceneInstanceService;
    private SceneComponentRelationService sceneComponentRelationService;
 
    public void setSceneComponentRelationService(SceneComponentRelationService sceneComponentRelationService) {
		this.sceneComponentRelationService = sceneComponentRelationService;
	}

	public void setSceneInstanceService(SceneInstanceService sceneInstanceService) {
        this.sceneInstanceService = sceneInstanceService;
    }

    public void setComponentService(ComponentService componentService) {
        this.componentService = componentService;
    }

    /**
     * 根据场景ID,获得场景对象
     *
     * @param id 场景实例ID
     * @return  As3Scene
     */
    public As3Scene loadScene(String id) {
        SceneInstance sceneInstance = sceneInstanceService.get(id);
        return convert(sceneInstance);
    }
    
    /**
     * 根据场景坐标,获得场景对象
     *
     * @param id 场景实例ID
     * @return  As3Scene
     */
    public As3Scene getScene(int x, int y){
    	return convert(sceneInstanceService.getInstanceByXY(x, y));
    }

    /**
     * 取得场景内的组件列表
     * @param sceneInstanceId 场景实例ID
     * @return
     */
    public List<As3Component> listComponent(String sceneInstanceId){
        SceneInstance sceneInstance = sceneInstanceService.get(sceneInstanceId);
        List<As3Component> lists = new ArrayList<As3Component>();
        List<SceneComponentRelation> list = sceneComponentRelationService.getListByCondition(sceneInstance.getId(),"depth");
        for (SceneComponentRelation c : list) {
            lists.add(convert(c));
        }
        return lists;
    }
    
    public As3Scene getSceneComponent(String id) {
    	As3Scene asScene = loadScene(id);
    	asScene.setComponentList(listComponent(id));
    	return asScene;
    }

    /**
     * 转换Component成As3Component
     * @param relation
     * @return
     */
    private As3Component convert(SceneComponentRelation relation){
        As3Component as = new As3Component();
        as.setName(relation.getName());
        as.setType(relation.getComponent().getType());
        as.setFunc(relation.getComponent().getFunc());
        as.setXmlDataComponent(relation.getComponent().getXmlData());
        as.setXmlDataRelation(relation.getXmlData());
        as.setStatus(relation.getComponent().getStatus());
        as.setUrl(Configure.configure().getAccessPath() + relation.getComponent().getResource().getPath());
        as.setX(relation.getX());
        as.setY(relation.getY());
        as.setDepth(relation.getDepth());
        as.setLayer(relation.getLayer());
        return as;
    }
    
    /**
     * 转换SceneInstance成As3Scene
     * @param relation
     * @return
     */
    private As3Scene convert(SceneInstance instance){
    	As3Scene as = new As3Scene();
        as.setName(instance.getName());
        as.setId(instance.getId());
        as.setSceneId(instance.getScene().getId());
        if(instance.getUpLevelScene()!=null)
        	as.setUpSceneId(instance.getUpLevelScene().getId());
        as.setDircX(instance.getDircX());
        as.setDircY(instance.getDircY());
        as.setCharx(instance.getScene().getCharx());
        as.setChary(instance.getScene().getChary());
        as.setBgUrl(Configure.configure().getAccessPath() + instance.getScene().getBackground().getPath());
        as.setOffsetY(instance.getScene().getOffsetY());
//        as.setMiniUrl(Configure.configure().getAccessPath() + instance.getScene().getMiniMap().getPath());
//        as.setIconUrl(Configure.configure().getAccessPath() + instance.getScene().getIconMap().getPath());
        return as;
    }
}
